<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :modal="false" :style="{ zIndex: 1010 }" :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px" @addCallBack="addCallBack">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="个人编号" prop="aac001">
            <el-input v-model="form.aac001" style="width: 370px;" :disabled="true" />
          </el-form-item>
          <el-form-item label="单位编号" prop="aab001">
            <!--            <el-input v-model="form.aab001" style="width: 370px;" />-->
            <unit-rpc ref="unitRpc" @unitRpcCallBack="unitRpcCallBack" />
          </el-form-item>
          <el-form-item label="建账年月">
            <el-date-picker v-model="form.aae206" format="yyyy-MM" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="参保日期">
            <el-date-picker v-model="form.aac030" format="yyyy-MM-dd" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="参保状态" prop="aac008">
            <el-select v-model="form.aac008" placeholder="请选择" style="width: 370px">
              <el-option v-for="item in dict.aac008" :key="item.id" :value="item.value" :label="item.label" />
            </el-select>
          </el-form-item>
          <el-form-item label="缴费状态" prop="aac031">
            <el-select v-model="form.aac031" placeholder="请选择" style="width: 370px">
              <el-option v-for="item in dict.aac031" :key="item.id" :value="item.value" :label="item.label" />
            </el-select>
          </el-form-item>
          <el-form-item label="参保险种" prop="aae140">
            <el-select v-model="form.aae140" placeholder="请选择" style="width: 370px">
              <el-option v-for="item in dict.aae140" :key="item.id" :value="item.value" :label="item.label" />
            </el-select>
          </el-form-item>
          <el-form-item label="工资" prop="aac040">
            <el-input v-model="form.aac040" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="首次参保年月" prop="aac049">
            <el-date-picker v-model="form.aac049" format="yyyy-MM" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="缴费开始年月" prop="aae041">
            <el-date-picker v-model="form.aae041" format="yyyy-MM" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="缴费截止年月" prop="aae042">
            <el-date-picker v-model="form.aae042" format="yyyy-MM" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.aae013" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="tableData" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="aaz159" label="参保关系id" fixed="left" />
        <el-table-column prop="aab001" label="单位编号" fixed="left" />
        <el-table-column prop="aae206" label="建账年月" :formatter="formatDate" />
        <el-table-column prop="aac030" label="参保日期" :formatter="formatDate" />
        <el-table-column prop="aac008" label="参保状态" :formatter="formatter" />
        <el-table-column prop="aac031" label="缴费状态" :formatter="formatter" />
        <el-table-column prop="aae140" label="参保险种" :formatter="formatter" />
        <el-table-column prop="aac040" label="工资" />
        <el-table-column prop="aac049" label="首次参保年月" :formatter="formatDate" />
        <el-table-column prop="aae041" label="缴费开始年月" :formatter="formatDate" />
        <el-table-column prop="aae042" label="缴费截止年月" :formatter="formatDate" />
        <!--        <el-table-column prop="aab359" label="经办机构" />-->
        <el-table-column prop="aac066" label="参保身份" />
        <el-table-column prop="aae013" label="备注" />
        <el-table-column v-if="checkPer(['admin','basicAc02:edit','basicAc02:del'])" label="操作" width="150px" align="center" fixed="right">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudBasicAc02 from '@/views/ynsb_comm/ac02/basicAc02'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import UnitRpc from '@/views/ynsb_comm/comm/module/unitRpc'

const defaultForm = { aaz159: null, aab001: null, aac001: null, aae206: null, aac030: null, aac008: '1', aac031: '1', aae140: null, aac040: null, aae041: null, aae042: null, aab359: null, aac066: null, aae013: null }
export default {
  name: 'Ac02',
  components: { UnitRpc, pagination, crudOperation, udOperation },
  props: {
    aac001: {
      type: String,
      required: true
    }
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '人员参保信息', url: 'api/basicAc02', idField: 'aaz159', sort: 'aaz159,desc', crudMethod: { ...crudBasicAc02 }})
  },
  // 数据字典
  dicts: ['aae140', 'aac008', 'aac031'],
  data() {
    return {
      permission: {
        add: ['admin', 'basicAc02:add'],
        edit: ['admin', 'basicAc02:edit'],
        del: ['admin', 'basicAc02:del']
      },
      rules: {
        aae140: [
          { required: true, message: '参保险种不能为空', trigger: 'blur' }
        ],
        aac049: [
          { required: true, message: '首次参保年月', trigger: 'blur' }
        ]
      },
      tableData: [] // 表格数据
    }
  },
  watch: {
    'crud.status.cu'(val) {
      if (this.crud.status.cu === '1') {
        setTimeout(() => {
          this.$refs.unitRpc.restAab001(this.form.aab001)
        }, 200) // 延迟1秒后显示弹窗
        this.form.aac001 = this.aac001
      } else {
        this.$refs.unitRpc.restAab001('')
        crudBasicAc02.getAc02({ aac001: this.aac001 }).then(res => {
          this.tableData = res
        })
      }
    }
  },
  mounted() {
  },
  methods: {
    formatter(row, column, cellValue) {
      // 根据 aae140 的值进行转码
      const key = column.property
      const data = this.dict[key]
      console.log(data, 'data')
      const dataList = data.filter(item => item.value === cellValue)
      if (dataList !== null && dataList !== [] && dataList !== undefined) {
        return dataList[0].label
      }
    },
    unitRpcCallBack(item) {
      this.form.aab001 = item.aab001
    },
    getAc02s() {
      crudBasicAc02.getAc02({ aac001: this.aac001 }).then(res => {
        this.tableData = res
      })
    },
    // 新增回调
    addCallBack() {
      this.$emit('queryCallBack')
    },
    // 日期格式转换
    formatDate(row, column, cellValue) {
      // 格式化日期为 YYYY-MM-DD
      if (cellValue !== null && cellValue !== undefined) {
        const date = new Date(cellValue)
        const year = date.getFullYear()
        const month = (date.getMonth() + 1).toString().padStart(2, '0')
        const day = date.getDate().toString().padStart(2, '0')
        return `${year}-${month}-${day}`
      } else {
        return '--'
      }
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped>

</style>
